import * as cc from 'cc';
import * as ccenv from 'cc/env'

import EventEmitter from '../libs/eventemitter/EventEmitter';
import ArrayWatcher, { WatcherEmit } from '../libs/watch/ArrayWatcher';
import { PokerConfig } from './config/PokerConfig';
import { TextureConfig } from './config/TextureConfig';
import PokerItem from './data/PokerItem';
import { Level } from './data/Level';
import { GameManager } from './manager/GameManager';
import Deck from './data/Deck';
import SnapFloor from '../libs/phaser/math/snap/SnapFloor.js';
import PokerArray from './data/PokerArray';
import { PokerHintDialog } from './game/PokerHintDialog';
import { notNull } from '../libs/utils/ExtUtils';
import { PokerIntensify } from './data/PokerIntensify';
import ValueWatcher from '../libs/watch/ValueWatcher';
import { UITransform } from 'cc';
import { NodeEventType } from 'cc';
import { MagicBuffer } from './manager/MagicManager';
import { PokerDetailDialog } from './game/PokerDetailDialog';
import { PokerStartDialog } from './game/PokerStartDialog';
import { director } from 'cc';
import { listenerClick } from './core/EventHelper';
import FragmentManager, { Config } from './core/gui/FragmentManager';
import { math } from 'cc';
import PokerCompose from './data/PokerCompose';
import LayoutManager from './manager/LayoutManager';


const isDevelopment = ccenv.DEBUG;
const isProduction = !ccenv.DEBUG;
const isPC = ccenv.MAC || ccenv.WINDOWS || ccenv.LINUX;
const EventClick = isPC ? cc.NodeEventType.MOUSE_DOWN : cc.NodeEventType.TOUCH_START;

const { ccclass, property, executeInEditMode } = cc._decorator;

@ccclass('Test')
export class Test extends cc.Component {

    emitter: EventEmitter = new EventEmitter()

    @property({ type: cc.Node })
    layout_debug: cc.Node = null!

    @property({ type: cc.Label })
    top_level_score: cc.Label = null!

    @property({ type: cc.Label })
    top_current_score: cc.Label = null!

    @property({ type: cc.Label })
    top_current_value: cc.Label = null!

    @property({ type: cc.Label })
    top_current_multiple: cc.Label = null!

    @property({ type: cc.Node })
    poker_magic_buffer_layout: cc.Node = null!

    @property({ type: cc.Node })
    poker_magic_handle_layout: cc.Node = null!

    @property({ type: cc.Node })
    poker_table_layout: cc.Node = null!

    @property({ type: cc.Node })
    poker_layout: cc.Node = null!

    @property({ type: cc.Node })
    play: cc.Node = null!

    @property({ type: cc.Node })
    replace: cc.Node = null!

    @property({ type: cc.Node })
    sortNumber: cc.Node = null!

    @property({ type: cc.Node })
    sortType: cc.Node = null!

    @property({ type: cc.Prefab })
    prefab: cc.Prefab = null!

    @property({ type: PokerHintDialog })
    dialogPokerHint: PokerHintDialog = new PokerHintDialog()

    @property({ type: PokerStartDialog })
    dialogPokerStart: PokerStartDialog = new PokerStartDialog();

    @property({ type: cc.Label })
    deck_recycle: cc.Label = null!;

    @property({ type: cc.Label })
    deck_mine: cc.Label = null!

    game: GameManager = null!

    fragmentManager: FragmentManager = null!;

    protected onLoad(): void {

        this.fragmentManager = new FragmentManager(this.node, new Config());

        this.game = new GameManager(this, new Level(1), Deck.defaultDeck);

        // 初始记分
        MagicBuffer.Magic_Base_Buffer.onActive(this.game);

        //#region Top
        // 当前等级分
        this.game.watchLevelScore.listenerChange((watch, curr, pre) => {
            this.top_level_score.string = `Boss : ${curr}`
        })
        this.top_level_score.string = `Boss : ${this.game.watchLevelScore.value}`;

        // 当前总分
        this.game.watchCurrentScore.listenerChange(async (watch, curr, pre) => {
            this.top_current_score.string = `得分::[${curr}]`
            const skipLevel = curr >= this.game.watchLevelScore.value;
            await this.game.emitSettlement(skipLevel).postRunning();
            if (skipLevel) {
                console.log('TODO:通关后续')
                this.game.gameNext()
            } else {
                console.log('TODO:下一局');
                this.gameStart(this.game);
            }
        })
        this.top_current_score.string = `得分::[${this.game.watchCurrentScore.value}]`

        // 当前分数
        this.game.watchCurrentValue.listenerChange((watch, curr, pre) => {
            this.top_current_value.string = `${curr}`
        })
        this.top_current_value.string = `${this.game.watchCurrentValue.value}`;
        const top_current_value_color = this.top_current_value.color.clone();
        // 当前倍率
        this.game.watchCurrentMultiple.listenerChange((watch, curr, pre) => {
            this.top_current_multiple.string = `${curr}`
        })
        this.top_current_multiple.string = `${this.game.watchCurrentMultiple.value}`
        const top_current_multiple_color = this.top_current_multiple.color.clone();

        // #region 预测
        // 当前 预测 分数 
        this.game.watchExpectValue.listenerChange((watch, curr, pre) => {
            console.log('watchExpectValue', curr)
            if (curr === 0) {
                this.top_current_value.string = `${this.game.watchCurrentValue.value}`;
                this.top_current_value.color = top_current_value_color;
            } else {
                this.top_current_value.string = `${this.game.watchCurrentValue.value}+(${curr})`
                this.top_current_value.color = math.Color.GREEN;
            }
            // updateExpectScore()
        })
        // 当前 预测 倍率
        this.game.watchExpectMultiple.listenerChange((watch, curr, pre) => {
            console.log('watchExpectMultiple', curr)
            if (curr === 0) {
                this.top_current_multiple.string = `${this.game.watchCurrentMultiple.value}`;
                this.top_current_multiple.color = top_current_multiple_color;
            } else {
                this.top_current_multiple.string = `${this.game.watchCurrentMultiple.value}+(${curr})`
                this.top_current_multiple.color = math.Color.GREEN;
            }
            // updateExpectScore()
        })


        //#region 牌库
        this.game.gashaponLibrary.listenerChange((watcher) => {
            this.deck_mine.string = `[ ${watcher.getItemSize()}/${watcher.getRemainSize()} ]`
        })
        this.deck_mine.string = `[ ${this.game.gashaponLibrary.getItemSize()}/${0} ]`

        this.game.gashaponRecycler.listenerChange((watcher, type) => {
            console.log('gashaponRecycler >>  ', type, watcher.getRemainSize(), watcher.getItemSize())
            this.deck_recycle.string = `[ ${watcher.getItemSize()}/${watcher.getRemainSize()} ]`
        })
        this.deck_recycle.string = `[ ${this.game.gashaponRecycler.getItemSize()}/${0} ]`

        //#region watchTable
        this.game.watchTable.listenerAdd(async (watch, add) => {
            if (add.length === 0) return;

            if (add[0].length() === 1) {

                // 将所有统一起来
                this.game.watchTable.foreach(v => {
                    v.pokers.forEach(p => {
                        p.pokerNode.removeFromParent();
                    })
                })
                // 清空布局
                this.poker_table_layout.destroyAllChildren();

                // 进行排序
                this.game.watchTable.sort((v1, v2) => {
                    const v1_value = PokerConfig.pokerNumber(v1.pokers[0].pokerIntensify.poker)
                    const v2_value = PokerConfig.pokerNumber(v2.pokers[0].pokerIntensify.poker)
                    return v2_value - v1_value
                }, false)
                // 重新添加


                // 添加组合
                this.game.watchTable.foreach(a => {
                    for (const element of a.pokers) {
                        this.poker_table_layout.addChild(element.pokerNode);
                    }
                })

                // 进行更新
                this.updateLayout(this.poker_table_layout);

                // poker compose
                const pokerCompose = new PokerCompose(this.game.watchTable.filter((v) => v.length() >= 3));

                //进行通知
                this.game.emitFinish(pokerCompose);

                // 开始统计
                this.game.emitStatisticCompose(pokerCompose);

                // 执行统计动画
                await this.game.postRunning();

                // 进行总分的计算
                this.game.watchCurrentScore.add(this.game.watchCurrentValue.value * this.game.watchCurrentMultiple.value)

                return;
            }
            // 预测结束
            expectReset();
            // 添加显示
            for (const pokerArray of add) {
                const node = new cc.Node(pokerArray.name());
                const layout = node.addComponent(cc.Layout);
                layout.type = cc.Layout.Type.HORIZONTAL;
                layout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
                layout.spacingX = -20;
                layout.horizontalDirection = cc.Layout.HorizontalDirection.LEFT_TO_RIGHT;
                this.poker_table_layout.addChild(node);
                pokerArray.pokers.sort((v1, v2) => {
                    const v1_value = PokerConfig.pokerNumber(v1.pokerIntensify.poker)
                    const v2_value = PokerConfig.pokerNumber(v2.pokerIntensify.poker)
                    return v2_value - v1_value
                })
                for (const element of pokerArray.pokers) {
                    node.addChild(element.pokerNode)
                }

                // 进行通知出牌完成
                this.game.emitPlay(pokerArray);

                //#region 通知记分
                // 进行单个牌的结算
                for (const pokerItem of pokerArray.pokers) {
                    // 发布消息，通知需要处理改牌的
                    this.game.emitStatisticSingle(pokerItem);
                }

                this.game.emitStatisticArray(pokerArray);
            }
        });

        this.game.watchTable.listenerRemove((watch, remove) => {
            for (const removeArray of remove) {
                removeArray.pokers.forEach(v => {
                    this.game.poolPokerRecycle(v);
                })
                this.poker_table_layout.getChildByName(removeArray.name())?.destroy();
            }
        });

        //#region watchMine
        this.game.watchMine.listenerUpdate((watch, emit) => {
            if (emit == WatcherEmit.Add || emit == WatcherEmit.Remove) {
                this.updateLayout(this.poker_layout);
            } else if (emit == WatcherEmit.Update) {
                // 排序
                this.poker_layout.removeAllChildren();
                watch.map(v => v.pokerNode).forEach(node => this.poker_layout.addChild(node));
            }
        });

        this.game.watchMine.listenerAdd((watch, add, idx) => {
            add.forEach(v => {
                this.poker_layout.addChild(v.pokerNode);
            });
        });
        this.game.watchMine.listenerRemove((watch, remove) => {
            remove.forEach(v => {
                this.game.poolPokerRecycle(v);
            });
        });

        //#region watchMineSelect
        this.game.watchMineSelect.listenerAdd((watch, add) => {
            add.forEach(v => {
                v.changeSelect(true)
            })
        });

        this.game.watchMineSelect.listenerRemove((watch, remove) => {
            remove.forEach(v => {
                v.changeSelect(false)
            })
        });

        this.game.watchMineSelect.listenerReplace((watch, replace, old) => {
            old.forEach(v => {
                v.changeSelect(false);
            })
            replace.forEach(v => {
                v.changeSelect(true);
            })
        });

        const expectReset = () => {
            this.game.watchExpectValue.reset();
            this.game.watchExpectMultiple.reset();
        }

        this.game.watchMineSelect.listenerUpdate((watch, emit) => {
            if (emit === WatcherEmit.Add || emit === WatcherEmit.Remove) {
                // 初始化分数
                expectReset();
                // 进行简单的算牌
                if (watch.length() === 3 || watch.length() === 4) {
                    const pokerExpectCall = (v: number, m: number) => {
                        this.game.watchExpectValue.add(v);
                        this.game.watchExpectMultiple.add(m);
                    }
                    const pokerItems = watch.getAll();
                    const pokerEnums = pokerItems.map(v => v.getPoker());
                    if (PokerConfig.pokerIsChain(pokerEnums) || PokerConfig.pokerIsPairs(pokerEnums)) {
                        // 进行简单的计分
                        // 进行单个牌的结算
                        for (const pokerItem of pokerItems) {
                            // 发布消息，通知需要处理改牌的
                            this.game.emitStatisticSingle(pokerItem, pokerExpectCall);
                        }
                        // 进行Poker Array的结算
                        this.game.emitStatisticArray(new PokerArray(pokerItems, this.game.watchReplacePoker.value > 0), pokerExpectCall);
                    }
                }
            }
        })


        //#region Mine Sort
        this.sortNumber.on(EventClick, () => {
            this.game.watchMineSelect.removeAll();
            this.game.watchMine.sort((v1, v2) => {
                const v1_value = PokerConfig.pokerValue(v1.pokerIntensify.poker)
                const v2_value = PokerConfig.pokerValue(v2.pokerIntensify.poker)
                return v2_value - v1_value
            });
        }, this);

        this.sortType.on(EventClick, () => {
            this.game.watchMineSelect.removeAll();
            this.game.watchMine.sort((v1, v2) => {
                const v1_value = PokerConfig.pokerValue(v1.pokerIntensify.poker) % 100
                const v2_value = PokerConfig.pokerValue(v2.pokerIntensify.poker) % 100
                return v2_value - v1_value
            });
        }, this);

        //#region Mine Replace
        this.replace.on(EventClick, () => {
            // 换牌
            if (this.game.watchMineSelect.length() === 0) return;

            const replaceCount = this.game.watchMineSelect.length();
            // 回收牌
            this.game.watchMineSelect.foreach(v => {
                this.game.poolPokerRecycle(v)
                this.game.pokerRecycler(v.pokerIntensify);
            })
            this.game.watchMineSelect.removeAll(false)
            // 换新牌
            this.schedule(() => {
                this.game.pokerNextItem();
            }, 0.2, replaceCount - 1);
        }, this);

        //#region Mine Play
        this.play.on(EventClick, async () => {
            this.pokerPlay(this.game, this.game.watchMineSelect.getAll());

            // const result = [
            //     // this.game .deck.getPokerIntensify(PokerConfig.B_4),
            //     // this.game .deck.getPokerIntensify(PokerConfig.B_7),
            //     // this.game .deck.getPokerIntensify(PokerConfig.J_B),
            //     // this.game .deck.getPokerIntensify(PokerConfig.J_S),

            //     this.game .deck.getPokerIntensify(PokerConfig.B_4),
            //     this.game .deck.getPokerIntensify(PokerConfig.B_6),
            //     this.game .deck.getPokerIntensify(PokerConfig.J_B),
            // ];

            // 测试 推荐提示 dialog
            // const value = await this.pokerHintDialog(this.game , result, async (to) => {

            // })

            // 测试 打牌
            // const node = new cc.Node('box');
            // const layout = node.addComponent(cc.Layout);

            // layout.type = cc.Layout.Type.HORIZONTAL;
            // layout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
            // layout.spacingX = -20;
            // layout.horizontalDirection = cc.Layout.HorizontalDirection.LEFT_TO_RIGHT;

            // this.poker_table_layout.addChild(node);

            // for (const element of result) {
            //     const poker = await this.game .poolPokerReuse(element);
            //     node.addChild(poker.pokerNode)
            // }

            // const pokerLayout = this.poker_table_layout.getComponent(cc.Layout);
            // pokerLayout.spacingX = 20;


            // 测试 Table
            // const pokerItemArray = []
            // for (const element of result) {
            //     const poker = await this.game .poolPokerReuse(element);
            //     pokerItemArray.push(poker);
            // }
            // this.game .watchTable.push(new PokerArray(pokerItemArray, false));

            // 测试删除牌
            // this.game .watchMine.removeIndex(0);

        }, this);

        const layout = new LayoutManager(this.node);

        this.addClick('推荐', () => {
            const pokerSuggest = this.game.checkMineAndTable();
            console.log(' 推荐 >> ', pokerSuggest)
            if (pokerSuggest.length === 0) {
                return;
            }
            this.game.watchMineSelect.replaceAll(pokerSuggest)
        })


        this.addClick('动画', () => {
            this.game.watchMine.get(0)?.changeActive(TextureConfig.Poker_Small_Color_Black.path)
            this.game.watchMine.get(1)?.changeActive(TextureConfig.Poker_Small_Color_Red.path)
            this.game.watchMine.get(2)?.changeActive(TextureConfig.Poker_Small_Link_Color_Red.path)
            this.game.watchMine.get(3)?.changeActive(TextureConfig.Poker_Small_Link_Color_Black.path)
            this.game.watchMine.get(4)?.changeActive(TextureConfig.Poker_Small_Link_Color_Cross.path)
            this.game.watchMine.get(5)?.changeActive(TextureConfig.Poker_H_Small.path)
            this.game.watchMine.get(6)?.changeActive(TextureConfig.Poker_B_Small.path)
            this.game.watchMine.get(7)?.changeActive(TextureConfig.Poker_F_Small.path)
            this.game.watchMine.get(8)?.changeActive(TextureConfig.Poker_M_Small.path)
        })

    }

    addClick(name: string, call: () => void) {
        const node = new cc.Node(name);
        const label = node.addComponent(cc.Label);
        label.string = name;
        label.fontSize = 20;
        label.lineHeight = 20;
        this.layout_debug.addChild(node)
        node.on(EventClick, call, this);
        return () => {
            node.destroy();
        }
    }

    start() {
        this.gameStart(this.game);
    }

    update(deltaTime: number) {

    }

    protected onDestroy(): void {
        console.log("onDestroy", this.play.eventProcessor)
        this.emitter.emit('onDestroy');
    }


    startScene() {
        if (!cc.assetManager.main) return;
        cc.assetManager.main.loadScene('launch', (err, scene) => {
            cc.director.runScene(scene)
        })
    }

    private async pokerPlayToTable(game: GameManager, playOut: PokerItem[], target: PokerIntensify[]) {
        if (game.watchMine.removeArray(playOut)) {
            // 出牌成功,清除并回收
            const result: PokerItem[] = [];
            for (const element of target) {
                const poker = await game.poolPokerReuse(element);
                result.push(poker)
            }
            // 将牌加入桌牌,并添加其是否是免费刷新前完成的,
            game.watchTable.push(new PokerArray(result, game.watchReplacePoker.value > 0));

            game.watchMineSelect.removeAll(false);
        }
    }

    //#region 出牌
    pokerPlay(game: GameManager, playOut: PokerItem[]) {
        if (playOut.length <= 0) return;

        const pokers = playOut.map(v => v.pokerIntensify);
        const pokerJoker = pokers.filter(v => PokerConfig.pokerIsJoker(v.poker));
        const pokerAce = pokers.filter(v => PokerConfig.pokerIsAce(v.poker));

        // 判断1张情况
        if (pokers.length === 1) {
            const tableCount = game.watchTable.length();
            // 判断::大小王
            if (pokerJoker.length === 1) {
                if (tableCount >= 1) {
                    this.pokerPlayToTable(game, playOut, [pokerJoker[0]])
                    return;
                }
            }
            // 判断::A
            if (pokerAce.length === 1) {
                if (tableCount >= 1) {
                    this.pokerPlayToTable(game, playOut, [pokerAce[0]])
                    return;
                }
            }
            //#region TODO 提示用户 还需要组牌到目标组合 才能成牌 
            this.fragmentManager.toast(`牌型未成${pokers[0].toString()}`);
        }
        // 判断3张情况,
        else if (pokers.length === 3) {
            // 存在若干鬼牌,提示用户想要的卡组
            if (pokerJoker.length > 0) {
                // 获取要变换的牌
                const isHasHint = this.dialogPokerHint.pokerHintDialog(game, pokers, (target) => {
                    this.pokerPlayToTable(game, playOut, target)
                });
                if (!isHasHint) {
                    //#region 无法根据现有的牌进行提示
                    this.fragmentManager.toast(`牌型不对,无推荐牌组${pokers.length}`);
                }
            } else {
                // 3张其他
                const pokerEnum = pokers.map(v => v.poker);
                if (PokerConfig.pokerIsPairs(pokerEnum)) {
                    // 判断对子
                    this.pokerPlayToTable(game, playOut, pokers)
                } else if (PokerConfig.pokerIsChain(pokerEnum)) {
                    // 判断链子
                    this.pokerPlayToTable(game, playOut, pokers)
                } else {
                    //#region 牌型不对,TODO 提示
                    this.fragmentManager.toast(`牌型不对，无法成牌${pokerEnum.map(v => PokerConfig.pokerIcon(v)).join(',')}`);
                }
            }
        }
        else if (pokers.length === 4) {
            // 判断是否具备糊了
            if (pokerJoker.length > 0) {
                const isHasHint = this.dialogPokerHint.pokerHintDialog(game, pokers, (target) => {
                    this.pokerPlayToTable(game, playOut, target)
                });
                if (!isHasHint) {
                    //#region 无法根据现有的牌进行提示
                    this.fragmentManager.toast(`牌型不对,无推荐牌组${pokers.length}`);
                }
            } else {
                // 判断杠子
                const pokerEnum = pokers.map(v => v.poker);
                if (PokerConfig.pokerIsPairs(pokerEnum)) {
                    this.pokerPlayToTable(game, playOut, pokers)
                } else if (PokerConfig.pokerIsChain(pokerEnum)) {
                    this.pokerPlayToTable(game, playOut, pokers)
                } else {
                    //#region 牌型不对,TODO 提示
                    this.fragmentManager.toast(`牌型不对，无法成牌${pokerEnum.map(v => PokerConfig.pokerIcon(v)).join(',')}`);
                }
            }
        } else {
            //#region 牌数不对,TODO 提示
            this.fragmentManager.toast('牌数不对');
        }
    }

    //#region gameStart
    gameStart(game: GameManager) {

        // 显示商店弹窗, 其中只会出售 buffer牌 和 handle牌
        // 到了Mine界面, 显示成长牌和卡包

        // 正常显示
        this.dialogPokerStart.show(game, (isNew) => {

            //新开局 
            if (isNew) {
                // 界面的回收
                game.watchTable.removeAll();
                // 手牌回收
                game.watchMine.removeAll();
                // 清空recycle lib
                game.gashaponRecycler.clear();
                // 重新填充 牌库
                game.gashaponLibrary.reset();
            } else {
                // 界面桌牌 的回收 回收到牌库
                const tableRemoveAll = game.watchTable.removeAll();
                for (const pokerArray of tableRemoveAll) {
                    for (const pokerItem of pokerArray.pokers) {
                        game.pokerRecycler(pokerItem.getPoker())
                    }
                }
                // 手牌回收
                const mineRemoveAll = game.watchMine.removeAll();
                for (const pokerItem of mineRemoveAll) {
                    game.pokerRecycler(pokerItem.getPoker(), game.gashaponLibrary);
                }
            }
            // 进行更新
            game.watchMineSelect.removeAll(false);
            // 移除分数
            game.watchCurrentValue.reset();
            game.watchCurrentMultiple.reset();
            game.watchExpectValue.reset();
            game.watchExpectMultiple.reset();

            this.resetMineLayout();
            this.resetTableLayout();

            // 重新发牌
            this.schedule(() => {
                // game.pokerNextItem(2, 3, 4, 5, 6);
                game.pokerNextItem(PokerConfig.B_A, PokerConfig.B_2, PokerConfig.B_3, PokerConfig.B_4, PokerConfig.B_5, 5, 6);
            }, 0.1, 9);
        });

        // this.schedule(() => {
        //     // game.pokerNextItem(2, 3, 4, 5, 6);
        //     game.pokerNextItem(PokerConfig.B_A, PokerConfig.B_2, PokerConfig.B_3, PokerConfig.B_4, PokerConfig.B_5, 5, 6);
        // }, 0.1, 9);

        //  测试PokerCompose
        // const createCompose = async (...target: (number | PokerConfig.EnumPoker)[][]) => {
        //     const resultArray: PokerArray[] = []
        //     for (const pokerEnumArray of target) {
        //         const result: PokerItem[] = [];
        //         for (const pokerEnum of pokerEnumArray) {
        //             const pokerItem = await game.pokerNextItem(pokerEnum);
        //             result.push(pokerItem)
        //         }
        //         resultArray.push(new PokerArray(result, false))
        //     }
        //     return new PokerCompose(resultArray)
        // }

        // const recycleCompose = (poker: PokerCompose) => {
        //     poker.arraies.forEach(array => {
        //         array.pokers.forEach(item => {
        //             item.pokerNode.destroy()
        //             game.pokerRecycler(item.pokerIntensify, game.gashaponLibrary)
        //         })
        //     })
        // }

        // this.dialogPokerStart.show(game, async () => {
        //     // 进行测试
        //     // 随机测试
        //     let compose: PokerCompose
        //     // compose = await createCompose([2, 3, 4], [5, 6, 7], [8, 9, 10]);
        //     // console.log(' 随机测试 ', compose.toString());

        //     // 同颜色测试
        //     compose = await createCompose(
        //         // [PokerConfig.B_2, PokerConfig.B_3, PokerConfig.B_4],
        //         // [PokerConfig.B_5, PokerConfig.B_6, PokerConfig.B_7],
        //         // [PokerConfig.F_8, PokerConfig.F_9, PokerConfig.F10],
        //         // [PokerConfig.F_5, PokerConfig.F_6, PokerConfig.F_7],
        //         // [PokerConfig.B_5, PokerConfig.B_6, PokerConfig.B_7],
        //         [5, 5, 5],
        //         [6, 6, 6],
        //         [7, 7, 7],
        //         [2, 2, 2],
        //         [3, 3, 3],
        //         [11, 11, 11, 11],
        //         [12, 12, 12, 12],
        //         [13, 13, 13, 13],
        //         [9, 9, 9, 9],);
        //     console.log(' 随机测试 ', compose.toString());

        //     // 同花色测试
        //     // 最大链子
        //     // 多条链子
        //     // 对子测试
        //     // 对子链子
        //     // 炸弹测试
        //     // 炸弹链子

        //     this.scheduleOnce(() => {
        //         recycleCompose(compose);
        //     })
        // });
    }

    //#region updateLayout
    updateLayout(node: cc.Node) {
        this.scheduleOnce(() => {
            const childSize = node.children.length;
            if (childSize <= 0) {
                return;
            }
            const parent = node.parent;
            if (!parent) return;
            const pokerLayout = node.getComponent(cc.Layout);
            if (!pokerLayout) return;
            const transform = node.children[0].getComponent(UITransform);
            if (!transform) return;
            const childWidth = transform.width;
            const pokerTransform = node.getComponent(cc.UITransform);
            if (!pokerTransform) return;
            const pokerParentTransform = parent.getComponent(cc.UITransform);
            if (!pokerParentTransform) return;

            const widthPokers = childSize * childWidth;
            const widthParent = pokerParentTransform.width;

            if (widthPokers >= SnapFloor(widthParent, childWidth)) {
                pokerLayout.resizeMode = cc.Layout.ResizeMode.NONE;
                pokerTransform.width = widthParent;
            } else {
                pokerLayout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
            }

            if (pokerLayout.resizeMode === cc.Layout.ResizeMode.NONE) {
                pokerLayout.spacingX = (SnapFloor((widthParent - childWidth) / (childSize - 1), 0.5)) - childWidth;
            }
        })
    }

    resetTableLayout() {
        const layout = this.poker_table_layout;
        const pokerLayout = layout.getComponent(cc.Layout);
        if (!pokerLayout) return;
        pokerLayout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
        pokerLayout.spacingX = 20;
    }

    resetMineLayout() {
        const layout = this.poker_layout;
        const pokerLayout = layout.getComponent(cc.Layout);
        if (!pokerLayout) return;
        pokerLayout.resizeMode = cc.Layout.ResizeMode.CONTAINER;
        pokerLayout.spacingX = 0;
    }
}

